package com.henu.zmb;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.henu.R;
import com.henu.zmb.DBhelper.DBhelper;
import com.henu.zmb.entity.Goods;
import com.henu.zmb.entity.User_Goods;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class GoodsDetails_Activity extends AppCompatActivity {
    ImageView stars;// 收藏按钮
    TextView title;
    TextView price;
    TextView time;
    TextView message;
    ImageView image;
    TextView phonenumber;
    ImageButton agd_return;
    boolean flag = false;
    User_Goods ug = new User_Goods();
    Goods good;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_details);
        stars = (ImageView) findViewById(R.id.agd_bcollection);
        title = (TextView)findViewById(R.id.agd_tvtitle);
        price = (TextView)findViewById(R.id.agd_tvprice);
        time = (TextView)findViewById(R.id.agd_tvtime);
        message = (TextView)findViewById(R.id.agd_tvmessage);
        image = (ImageView)findViewById(R.id.agd_ivimage);
        phonenumber = (TextView)findViewById(R.id.agd_tphonenumber);
        agd_return = (ImageButton)findViewById(R.id.agd_return) ;
        Intent intent = getIntent();
        good = (Goods)intent.getSerializableExtra("good");
        title.setText(good.getGoods_Name());
        time.setText(good.getCreatedAt());
        price.setText("¥："+good.getGoods_Price());
        message.setText(good.getGoods_Message());
        phonenumber.setText(good.getUser_Id().getMobilePhoneNumber());
        // 图片显示
        String url = (good.getGoods_Image().getUrl()); // 获取地址
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();

        ImageLoader.getInstance().displayImage(url, image, options);
        // 收藏
        // 初始化
        User_Goods user_goods;
        List<BmobQuery<User_Goods>> queries = new ArrayList<BmobQuery<User_Goods>>();
        BmobQuery<User_Goods> query1 = new BmobQuery<User_Goods>();
        // 用户
        query1.addWhereEqualTo("User_Id", new BmobPointer(BmobUser.getCurrentUser()));
        BmobQuery<User_Goods> query2 = new BmobQuery<User_Goods>();
        // 物品
        query2.addWhereEqualTo("Goods_Id",new BmobPointer(good));
        queries.add(query1);// 条件1
        queries.add(query2);// 条件2
        BmobQuery<User_Goods> query = new BmobQuery<User_Goods>();
        query.and(queries);
        query.include("User_Id");
        query.include("Goods_Id");
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        query.findObjects(new FindListener<User_Goods>() {
            @Override
            public void done(List<User_Goods> object, BmobException e) {
                if(e == null && object.size()!=0){
                    flag = true;// 已经收藏
                    stars.setImageResource(R.mipmap.stars1);
                    ug = object.get(0);
                }else{
                   flag = false;// 没有收藏
                    ug.setUser_Id(BmobUser.getCurrentUser());
                    ug.setGoods_Id(good);
                    stars.setImageResource(R.mipmap.stars);
                }
            }
        });
        final DBhelper<User_Goods> db = new DBhelper<User_Goods>();
        stars.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 收藏
                if(flag == false){
                    ug.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            flag = true;
                            stars.setImageResource(R.mipmap.stars1);
                        }
                    });

                }else{
                    // 取消收藏
                    ug.delete(new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            stars.setImageResource(R.mipmap.stars);
                            flag = false;
                        }
                    });
                }

            }
        });
        agd_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
